/*******************************************************************************
 * The MOSES project, model based software engineering of networked embedded systems.
 * 
 * Copyright (C) 2010-2013, University of Applied Sciences Wildau,
 * Telematics Research Group, http://www.tm.th-wildau.de/research-group
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses>.
 * 
 * Contributors:
 *     University of Applied Sciences Wildau - initial API and implementation
 * 
 * If you are interested in a commercial license please contact us via 
 * e-mail: ralf.vandenhouten@th-wildau.de.
 ******************************************************************************/
package de.thwildau.tm.moses.examples.nmea.app;

import java.util.List;

import javax.swing.JFrame;

/**
 * @author Thomas Kistel
 */
public class SerialPortFrame extends javax.swing.JDialog {

    private static final long serialVersionUID = 1L;


    /**
     * Creates new form SerialPortFrame
     */
    public SerialPortFrame() {
        initComponents();
    }

    /**
     * Creates new form SerialPortFrame
     * 
     * @param parent Parentframe
     */
    public SerialPortFrame(SerialPortConnecter spc, JFrame f) {
        this.spc = spc;
        this.setLocationRelativeTo(f);
        initComponents();
    }

    /**
     * This method is called from within the constructor to initialize the form. WARNING: Do NOT modify this
     * code. The content of this method is always regenerated by the Form Editor.
     */
    private void initComponents() {

        discriptionLabel = new javax.swing.JLabel();
        portCombo = new javax.swing.JComboBox<String>();
        submitButton = new javax.swing.JButton();
        exitButton = new javax.swing.JButton();
        infoLabel = new javax.swing.JLabel();

        infoLabel.setForeground(new java.awt.Color(255, 0, 0));
        infoLabel.setText("Scanning available ports!");
        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);
        setModal(true);

        discriptionLabel.setText("Choose port for connection:");
        discriptionLabel.setToolTipText("The serial port where the device is connectet.");

        new Thread() {

            public void run() {
                List<String> sList = spc.getSerialCommPortIdentifiers();

                String[] tmp = new String[sList.size()];
                for (int i = 0; i < sList.size(); i++) {
                    tmp[i] = sList.get(i);
                }
                if (tmp.length > 0) {
                    infoLabel.setVisible(false);
                    portCombo.setModel(new javax.swing.DefaultComboBoxModel<String>(tmp));
                } else {
                    infoLabel.setText("Can't find a serial device!");
                }
            };
        }.start();

        submitButton.setText("Submit");
        submitButton.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                submitButtonActionPerformed(evt);
            }
        });

        exitButton.setText("Exit");
        exitButton.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exitButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                layout.createSequentialGroup().addContainerGap().addGroup(
                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(
                                discriptionLabel, javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addGroup(
                                layout.createSequentialGroup().addComponent(submitButton).addPreferredGap(
                                        javax.swing.LayoutStyle.ComponentPlacement.RELATED,
                                        javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE).addComponent(exitButton)).addGroup(
                                layout.createSequentialGroup().addGroup(
                                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                                                layout.createSequentialGroup().addGap(39, 39, 39).addComponent(
                                                        portCombo, javax.swing.GroupLayout.PREFERRED_SIZE,
                                                        javax.swing.GroupLayout.DEFAULT_SIZE,
                                                        javax.swing.GroupLayout.PREFERRED_SIZE)).addComponent(infoLabel)).addGap(
                                        0, 0, Short.MAX_VALUE))).addContainerGap()));
        layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(
                layout.createSequentialGroup().addContainerGap().addComponent(discriptionLabel).addPreferredGap(
                        javax.swing.LayoutStyle.ComponentPlacement.UNRELATED).addComponent(portCombo,
                        javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                        javax.swing.GroupLayout.PREFERRED_SIZE).addPreferredGap(
                        javax.swing.LayoutStyle.ComponentPlacement.RELATED, 10, Short.MAX_VALUE).addComponent(infoLabel).addPreferredGap(
                        javax.swing.LayoutStyle.ComponentPlacement.RELATED).addGroup(
                        layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE).addComponent(
                                submitButton).addComponent(exitButton)).addContainerGap()));

        pack();
    }

    private void submitButtonActionPerformed(java.awt.event.ActionEvent evt) {
        spc.connect((String)portCombo.getSelectedItem());
        this.setVisible(false);
        this.dispose();
    }

    private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {
        this.setVisible(false);
        this.dispose();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(SerialPortFrame.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(SerialPortFrame.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(SerialPortFrame.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(SerialPortFrame.class.getName()).log(java.util.logging.Level.SEVERE,
                    null, ex);
        }

        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new SerialPortFrame().setVisible(true);
            }
        });
    }


    private javax.swing.JLabel discriptionLabel;
    private javax.swing.JButton exitButton;
    private javax.swing.JLabel infoLabel;
    private javax.swing.JComboBox<String> portCombo;
    private javax.swing.JButton submitButton;
    private SerialPortConnecter spc;
}
